import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/page/delay/delay_controller.dart';
import 'package:flutter_soundmagus/page/delay/delay_item.dart';
import 'package:flutter_soundmagus/util/util.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:flutter_soundmagus/widget/menu_bottom_bar.dart';
import 'package:flutter_soundmagus/widget/page_base.dart';
import 'package:flutter_soundmagus/widget/widget.dart';
import '../inout/summing_controller.dart';
import '../inout/widget/output_graph.dart';
import 'package:get/get.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

///延时页面
class PageDelay extends PageBase {
  const PageDelay({Key? key}) : super(key: key);

  @override
  _PageDelayState createState() => _PageDelayState();
}

class _PageDelayState extends PageBaseState<PageDelay> {
  @override
  bool get menuHidden => false;

  @override
  void initState() {
    super.initState();

    Get.put(DelayController());
  }

  @override
  Widget buildBody(BuildContext context) {
    return DelayView();
  }
}

class DelayView extends GetView<DelayController> {
  DelayView({Key? key}) : super(key: key);
  //单位
  final units = UnitType.ms.names;

  final focusNodes = List.generate(kChCount, (index) => FocusNode());

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
      keyboardBarColor: Colors.grey[200],
      nextFocus: false,
      actions: [
        ...focusNodes.map((e) => KeyboardActionsItem(
              focusNode: e,
            )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    print('build delayview');
    return Column(
      children: [
        Expanded(
          child: /*KeyboardActions(
              config: _buildConfig(context),
              child:*/
              Padding(
            padding: EdgeInsets.only(left: Get.mediaQuery.padding.left),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: List.generate(5, (index) {
                if (index == 2) {
                  //中间空隙
                  return SizedBox(
                    width: 120.w,
                    height: 120.w,
                    child: Transform.scale(
                      scale: 1.65,
                      child: Obx(() => OutputGraph(
                            mini: true &&
                                Get.find<SummingController>().v.value >= 0,
                            miniScale: 0.3,
                            customFontSize: () => 3.0,
                            mode: Get.find<SummingController>()
                                .outputModeLast
                                .value,
                            leftDrive:
                                Get.find<SummingController>().leftDrive.value,
                            selecteds: controller.delayItemControllers
                                .map((e) => false)
                                .toList()
                              ..[controller.selectCH.value] = true,
                            onChanged: (v) => controller.setChannel(v),
                          )),
                    ),
                  );
                }
                var ch = index * 2;
                if (index > 2) {
                  ch = (index - 1) * 2;
                }
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 35),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      SizedBox(
                        width: 120.w,
                        height: 120.w,
                        child: DelayItem(
                          index: ch,
                          focusNode: focusNodes[0],
                        ),
                      ),
                      SizedBox(
                        width: 120.w,
                        height: 120.w,
                        child: DelayItem(
                          index: ch + 1,
                          focusNode: focusNodes[0],
                        ),
                      ),
                    ],
                  ),
                );
              }),
            ),
          ),
        ),
        _bottomNav(),
      ],
    );
  }

  Align _bottomNav() {
    return Align(
      alignment: Alignment.topLeft,
      child: MenuBottomBar(title: 'Delay'),
    );
  }
}
